package com.alibaba.citrus.ots.price.repository.impl;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.price.ability.functions.constant.ActivityOrderTagEnum;
import com.alibaba.citrus.ots.price.repository.ActivityQueryRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsforecastsales.activityuserecordrelation.dto.QueryActivityUseRecordRelationResponse;
import com.epoch.app.otsforecastsales.activityuserecordrelation.dto.QueryIsExistRecordResponse;
import com.epoch.app.otsforecastsales.activityuserecordrelation.service.ActivityUseRecordRelationService;
import com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryActivityIdByClientIdResponse;
import com.epoch.app.otsforecastsales.domian.activityclientrelation.service.ActivityClientRelationService;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryActivityAndItemMapRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryActivityAndItemMapResponse;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryActivityIdsByActivityTermIdsRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryGiftByActivityRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryGiftByActivityResponse;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryItemByActivityRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryItemByActivityResponse;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryTermSDOListByActivityIdRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.dto.QueryTermSDOListByTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activityterm.model.ActivityTerm;
import com.epoch.app.otsforecastsales.domian.activityterm.service.ActivityTermService;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityAndGiftMapForExportRequest;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityAndGiftMapForExportResponse;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityAndGiftMapRequest;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityAndGiftMapResponse;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityIdAndTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryActivityIdAndTermIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryGiftByActivityIdAndTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryGiftByActivityIdAndTermIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.dto.QueryTermAndGiftRelationListResponse;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.model.ActivityTermGiftRelation;
import com.epoch.app.otsforecastsales.domian.activitytermgiftrelation.service.ActivityTermGiftRelationService;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryActivityAndItemMapForExportRequest;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryActivityAndItemMapForExportResponse;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryItemByActivityIdAndTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryItemByActivityIdAndTermIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryTermAndItemRelationByTermIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryTermAndItemRelationByTermIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytermitemrelation.service.ActivityTermItemRelationService;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryActivityAndCustomerMapRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryActivityAndCustomerMapResponse;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryActivityTitleByActivityIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryActivityTitleIdListByClientIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryCustomerByActivityIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryCustomerByActivityIdResponse;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryGroupControlActivityIdAndNoteIdRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryGroupControlActivityIdsRequest;
import com.epoch.app.otsforecastsales.domian.activitytitle.dto.QueryGroupControlActivityIdsResponse;
import com.epoch.app.otsforecastsales.domian.activitytitle.model.ActivityTitle;
import com.epoch.app.otsforecastsales.domian.activitytitle.service.ActivityTitleService;
import com.epoch.app.otsforecastsales.domian.activitytitleext.dto.QueryCanJoinActivityRequest;
import com.epoch.app.otsforecastsales.domian.activitytitleext.model.ActivityTitleExt;
import com.epoch.app.otsforecastsales.domian.activitytitleext.service.ActivityTitleExtService;
import com.epoch.app.otsforecastsales.domian.operatejournalflow.model.OperateJournalFlow;
import com.epoch.app.otsforecastsales.domian.operatejournalflow.service.OperateJournalFlowService;
import com.epoch.app.otsforecastsales.domian.segmentedactivityterm.dto.LoadListResponse;
import com.epoch.app.otsforecastsales.domian.segmentedactivityterm.dto.QuerySegmentedActivityTermListRequest;
import com.epoch.app.otsforecastsales.domian.segmentedactivityterm.dto.QuerySegmentedActivityTermListResponse;
import com.epoch.app.otsforecastsales.domian.segmentedactivityterm.service.SegmentedActivityTermService;
import com.epoch.app.otsforecastsales.model.dto.*;
import com.epoch.app.otsforecastsales.ofsactivitytermindex.dto.*;
import com.epoch.app.otsforecastsales.ofsactivitytermindex.service.OfsActivityTermIndexService;
import com.epoch.app.otsforecastsales.ofsactivitytermrelationindex.dto.QueryActivityIdsByCustomerOrItemRequest;
import com.epoch.app.otsforecastsales.ofsactivitytermrelationindex.dto.QueryActivityIdsByCustomerOrItemResponse;
import com.epoch.app.otsforecastsales.ofsactivitytermrelationindex.service.OfsActivityTermRelationIndexService;
import com.epoch.app.otsplatformenhance.enums.CommStatusEnum;
import com.epoch.app.otspricecenter.dto.request.ActivityInfoRequest;
import com.epoch.app.otspricecenter.dto.request.CoverActivityInfoRequest;
import com.epoch.app.otspricecenter.dto.request.ParticipateActivityInfoRequest;
import com.epoch.app.otspricecenter.dto.response.ActivityTermViewResponse;
import com.epoch.app.otspricecenter.model.dto.ActivityGiftItemRelationSDO;
import com.epoch.app.otspricecenter.model.dto.ActivityTermIndexSDO;
import com.epoch.app.otspricecenter.model.dto.SegmentedActivityCombinationSDO;
import com.epoch.app.otspricecenter.model.enums.ActivityStatusEnum;
import com.epoch.app.otspricecenter.model.enums.ActivityTypeEnum;
import com.epoch.app.otspricecenter.sdo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: qiaojiange
 * @create: 2021/10/19
 * @Description 活动查询
 */
@Repository
public class ActivityQueryRepositoryImpl implements ActivityQueryRepository {

    private static Log log = Log.getLogger(ActivityQueryRepositoryImpl.class);

    @Resource
    private ActivityTitleService activityTitleService;

    @Resource
    private ActivityTermService activityTermService;

    @Resource
    private ActivityClientRelationService activityClientRelationService;

    @Resource
    private ActivityTitleExtService activityTitleExtService;

    @Resource
    private SegmentedActivityTermService segmentedActivityTermService;

    @Resource
    private ActivityTermItemRelationService termItemRelationService;

    @Resource
    private ActivityTermGiftRelationService termGiftRelationService;

    @Resource
    private OfsActivityTermIndexService ofsActivityTermIndexService;

    @Resource
    private ActivityUseRecordRelationService activityUseRecordRelationService;

    @Resource
    private OperateJournalFlowService operateJournalFlowService;

    @Resource
    private ActivityTermItemRelationService activityTermItemRelationService;

    @Resource
    private ActivityTermGiftRelationService activityTermGiftRelationService;

    @Resource
    private OfsActivityTermRelationIndexService ofsActivityTermRelationIndexService;

    //活动状态
    private static Integer activityEnabled = 1;

    private static Integer activityDisabled = 2;


    @Override
    @RepositoryInvoker(remark = "查询活动抬头", printLog = true, errorCode = "OTS-08-001-01-16-017")
    public Set<ActivityTitleSDO> queryActivityTitle(ActivityInfoRequest request) {
//        1. 根据客户 ID 查询活动抬头表，获取活动id
//        2. 客户 ID查询活动客户关系表中的活动 id
//        3. 根据送达方ID 查询活动抬头表，获取活动id
//        4. 根据送达方ID 查询活动客户关系表中的活动 id
//        5. 以上 4 种情况，求并集
//        6. 根据 活动id列表查询活动抬头

        //1. 根据客户 ID 查询活动抬头表，获取活动id
        Set<String> set = new HashSet<>();
        if (StringUtils.isNotBlank(request.getClientId())
                && Objects.nonNull(request.getActivityUseTime())
                && Objects.nonNull(request.getDeliveryDate())) {
            QueryActivityTitleIdListByClientIdRequest clientIdRequest = new QueryActivityTitleIdListByClientIdRequest();
            clientIdRequest.setClientId(request.getClientId());
            clientIdRequest.setActivityUseTime(request.getActivityUseTime());
            clientIdRequest.setDeliveryDate(request.getDeliveryDate());
            clientIdRequest.setSalesOrganization(request.getSalesOrganization());
            clientIdRequest.setBusinessChannel(request.getBusinessChannel());
            QueryActivityTitleIdListByClientIdResponse idListByClientId = activityTitleService.queryActivityTitleIdListByClientId(clientIdRequest);
            if (Objects.nonNull(idListByClientId) && CollectionUtils.isNotEmpty(idListByClientId.getIdList())) {
                set.addAll(idListByClientId.getIdList());
            }
        }

        //2. 客户 ID查询活动客户关系表中的活动 id
        if (StringUtils.isNotBlank(request.getClientId())) {
            ActivityClientRelationCreateRequest createRequest = new ActivityClientRelationCreateRequest();
            createRequest.setClientId(request.getClientId());
            QueryActivityIdByClientIdResponse idByClientIdResponse = activityClientRelationService.queryActivityIdByClientId(createRequest);

            if (Objects.nonNull(idByClientIdResponse)
                    && CollectionUtils.isNotEmpty(idByClientIdResponse.getIdList())) {
                set.addAll(idByClientIdResponse.getIdList());
            }
        }

        //3. 根据送达方ID 查询活动抬头表，获取活动id
        if (StringUtils.isNotBlank(request.getShipAddressId())
                && Objects.nonNull(request.getActivityUseTime())
                && Objects.nonNull(request.getDeliveryDate())) {
            QueryActivityTitleIdListByClientIdRequest clientIdRequest = new QueryActivityTitleIdListByClientIdRequest();
            clientIdRequest.setClientId(request.getShipAddressId());
            clientIdRequest.setActivityUseTime(request.getActivityUseTime());
            clientIdRequest.setDeliveryDate(request.getDeliveryDate());
            clientIdRequest.setSalesOrganization(request.getSalesOrganization());
            clientIdRequest.setBusinessChannel(request.getBusinessChannel());
            QueryActivityTitleIdListByClientIdResponse idListByShipAddressId = activityTitleService.queryActivityTitleIdListByClientId(clientIdRequest);
            if (Objects.nonNull(idListByShipAddressId) && CollectionUtils.isNotEmpty(idListByShipAddressId.getIdList())) {
                set.addAll(idListByShipAddressId.getIdList());
            }
        }

        //4. 根据送达方ID 查询活动客户关系表中的活动 id
        if (StringUtils.isNotBlank(request.getShipAddressId())) {
            ActivityClientRelationCreateRequest createRequest = new ActivityClientRelationCreateRequest();
            createRequest.setClientId(request.getShipAddressId());
            QueryActivityIdByClientIdResponse idByClientIdResponse = activityClientRelationService.queryActivityIdByClientId(createRequest);

            if (Objects.nonNull(idByClientIdResponse)
                    && CollectionUtils.isNotEmpty(idByClientIdResponse.getIdList())) {
                set.addAll(idByClientIdResponse.getIdList());
            }
        }

        if (CollectionUtils.isEmpty(set)) {
            log.error("未查询到可用活动 = {}", request);
            return new HashSet<>();
        }
        //根据活动抬头列表查询活动抬头
        QueryActivityTitleByIdListRequest idListRequest = new QueryActivityTitleByIdListRequest();
        idListRequest.setActivityIdList(set.stream().collect(Collectors.toList()));
        idListRequest.setActivityUseTime(request.getActivityUseTime());
        idListRequest.setDeliveryDate(request.getDeliveryDate());
        idListRequest.setSalesOrganization(request.getSalesOrganization());
        idListRequest.setBusinessChannel(request.getBusinessChannel());
        ActivityTitleLoadListResponse listResponse = activityTitleService.queryActivityTitleByIdList(idListRequest);

        Map<String, ActivityTitleSDO> map = new HashMap<>();
        if (Objects.nonNull(listResponse) && CollectionUtils.isNotEmpty(listResponse.getData())) {
            List<ActivityTitle> tempActivityList = new ArrayList<>();

            //按组管控标识处理，这里查询活动抬头扩展表，过滤出活动剩余费用和活动剩余量大于0的活动抬头
//            Map<String, List<ActivityTitle>> activityTitleTmpMap = listResponse.getData().stream().collect(Collectors.groupingBy(ActivityTitle::getControlByGroupFlag));
            List<ActivityTitle> groupControllerList = listResponse.getData().stream().filter(e->StringUtils.equals("X",e.getControlByGroupFlag())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(groupControllerList)) {
                List<String> activityIds = groupControllerList.stream().map(ActivityTitle::getActivityId).collect(Collectors.toList());
                Map<String, ActivityTitle> groupControlActivityMap = groupControllerList.stream()
                        .collect(Collectors.toMap(e ->e.getActivityId()+"_"+e.getActivityNoteId(), Function.identity()));

                QueryCanJoinActivityRequest queryCanJoinActivityRequest = new QueryCanJoinActivityRequest();
                queryCanJoinActivityRequest.setActivityNoteIds(groupControllerList.stream()
                        .map(ActivityTitle::getActivityNoteId).collect(Collectors.toList()));
                //该方法已经筛选出活动申请量大于活动占用量的数据了，下面的只需要把补货活动单独处理下即可
                Result<List<ActivityTitleExt>> activityTitleExtResult = activityTitleExtService.queryCanJoinActivity(queryCanJoinActivityRequest);

                //筛选出非补货活动
                //补货订单处理
                detailReplenishment(activityTitleExtResult,groupControllerList);

                //先移除掉所有的分组管控活动后，添加能用的分组管控活动
                listResponse.getData().removeAll(groupControllerList);
                if (Objects.nonNull(activityTitleExtResult) && CollectionUtils.isNotEmpty(activityTitleExtResult.getResult())) {
                    Map<String, ActivityTitleExt> activityTitleExtMap = activityTitleExtResult.getResult()
                            .stream().collect(Collectors.toMap(ActivityTitleExt::getActivityNoteId, Function.identity()));
                    activityTitleExtResult.getResult().forEach(e -> {
                        for (String activityId : activityIds) {
                            ActivityTitle activityTitle = groupControlActivityMap.get(activityId + "_" + e.getActivityNoteId());
                            if (Objects.nonNull(activityTitle)) {
                                listResponse.getData().add(activityTitle);
                            }
                        }

//                        activityTitle.setApplyFee(activityTitleExtMap.get(e.getActivityNoteId()).getApplyFee());
                    });
                }

            }


            //是补货订单进行过滤出补货类型的活动，其他类型的订单不用处理
            if (ActivityOrderTagEnum.REPLENISH_ORDER.getCode().equals(request.getOrderType())) {
                tempActivityList = listResponse.getData().stream().filter(e -> {
                    return ActivityTypeEnum.REPLENISHMENT.getValue().equals(e.getActivityFacadeCode());
                }).collect(Collectors.toList());
            } else {
                tempActivityList = listResponse.getData();
            }

            tempActivityList.forEach(title -> {
                ActivityTitleSDO sdo = new ActivityTitleSDO();
                BeanUtils.copyProperties(title, sdo);
                map.put(sdo.getActivityId(), sdo);
            });
        }
        return map.values().stream().collect(Collectors.toSet());
    }

    /**
     * 补货活动处理
     * @param activityTitleExtResult
     */
    private void detailReplenishment(Result<List<ActivityTitleExt>> activityTitleExtResult,List<ActivityTitle> groupControllerList) {

        List<ActivityTitleExt> activityTitleExts = new ArrayList<>();
        //筛选出非补货活动数据
        List<String> notReplenishmentList = new ArrayList<>(groupControllerList.stream().filter(e ->
                        !ActivityTypeEnum.REPLENISHMENT.getValue().equals(e.getActivityFacadeCode()))
                .map(ActivityTitle::getActivityNoteId).collect(Collectors.toSet()));

        if (Objects.nonNull(activityTitleExtResult) && CollectionUtils.isNotEmpty(activityTitleExtResult.getResult())) {
            activityTitleExtResult.getResult().forEach(e -> {
                if (notReplenishmentList.contains(e.getActivityNoteId())) {
                    if (e.getApplyItemQuantity() > e.getActivityOccupyQuantity()) {
                        activityTitleExts.add(e);
                    }
                } else {
                    activityTitleExts.add(e);
                }
            });
        }
        activityTitleExtResult.setResult(activityTitleExts);
    }

    @Override
    public Set<ActivityTermSDO> queryTermSDOList(String obtainActivityId) {
//        1. 根据活动 id 和商品信息查询符合条件 termSDO
        QueryTermSDOListByActivityIdRequest createRequest = new QueryTermSDOListByActivityIdRequest();
        createRequest.setActivityId(obtainActivityId);
        QueryTermSDOListByActivityIdResponse queryTermSDOListByActivityIdResponse = activityTermService.queryTermSDOListByActivityId(createRequest);
        Set<ActivityTermSDO> set = new HashSet<>();
        if (Objects.nonNull(queryTermSDOListByActivityIdResponse)
                && CollectionUtils.isNotEmpty(queryTermSDOListByActivityIdResponse.getTermList())) {
            for (ActivityTermLoadListVO term : queryTermSDOListByActivityIdResponse.getTermList()) {
                ActivityTermSDO termSDO = new ActivityTermSDO();
                BeanUtils.copyProperties(term, termSDO);
                set.add(termSDO);
            }
        }
        return set;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public ActivityTitleExtSDO queryTitleExt(String obtainActivityNoteId) {
        ActivityTitleExtCreateRequest createRequest = new ActivityTitleExtCreateRequest();
        createRequest.setActivityNoteId(obtainActivityNoteId);
        ActivityTitleExt activityTitleExt = activityTitleExtService.queryActivityTitleExt(createRequest);
        ActivityTitleExtSDO activityTitleExtSDO = new ActivityTitleExtSDO();
        BeanUtils.copyProperties(activityTitleExt, activityTitleExtSDO);
        return activityTitleExtSDO;
    }

    @Override
    public QueryPageActivityTermIndexResponse queryPromotionPageList(ActivityTermIndexSDO sdo) {

        OfsActivityTermIndexLoadListVO ofsActivityTermIndexLoadListVO = new OfsActivityTermIndexLoadListVO();

        BeanUtils.copyProperties(sdo, ofsActivityTermIndexLoadListVO);

        ofsActivityTermIndexLoadListVO.setSubActivityId(sdo.getActivityId());

        ofsActivityTermIndexLoadListVO.setMainActivityNoteName(sdo.getActivityNoteName());

        //活动类型编码
        ofsActivityTermIndexLoadListVO.setMainActivityFacadeCode(sdo.getActivityTypeId());

        //活动状态判断
        if (StringUtils.isNotBlank(sdo.getActivityStatus())) {

            //页面调整,不再需要下列几个筛选条件
//            if (sdo.getActivityStatus().equals(ActivityStatusEnum.ACTIVITY_NOT_BEGIN.getValue())) {
//                //活动未开始
//                ofsActivityTermIndexLoadListVO.setActivityNotBegin(new Date().getTime());
//            }
//            if (sdo.getActivityStatus().equals(ActivityStatusEnum.ACTIVITY_DOING.getValue())) {
//                //活动进行中
//                ofsActivityTermIndexLoadListVO.setActivityDoing(new Date().getTime());
//            }
//            if (sdo.getActivityStatus().equals(ActivityStatusEnum.ACTIVITY_OVER.getValue())) {
//                //活动已结束
//                ofsActivityTermIndexLoadListVO.setActivityOver(new Date().getTime());
//            }
            //上述条件筛选的是未禁用的活动,因为PD说页面上活动状态只保留启用禁用的查询功能,所以这条代码注释掉
            //ofsActivityTermIndexLoadListVO.setSubStatus(CommStatusEnum.ENABLE.getCode());

            //活动已禁用
            if (sdo.getActivityStatus().equals(ActivityStatusEnum.ACTIVITY_DISABLED.getValue())) {
                ofsActivityTermIndexLoadListVO.setSubStatus(activityDisabled);
            }

            //活动已启用
            if (sdo.getActivityStatus().equals(ActivityStatusEnum.ACTIVITY_ENABLED.getValue())) {
                ofsActivityTermIndexLoadListVO.setSubStatus(activityEnabled);
            }



        }
        //ofsActivityTermIndexLoadListVO.setSubStatus(sdo.getActivityStatus());
        ofsActivityTermIndexLoadListVO.setMainActivityStartDate(sdo.getActivityStartDate());
        ofsActivityTermIndexLoadListVO.setMainActivityEndDate(sdo.getActivityEndDate());
        ofsActivityTermIndexLoadListVO.setMainOrderStartDate(sdo.getOrderStartDate());
        ofsActivityTermIndexLoadListVO.setMainClientId(sdo.getClientId());
        ofsActivityTermIndexLoadListVO.setMainSalesOrganization(sdo.getSalesOrganizationId());
        ofsActivityTermIndexLoadListVO.setMainSalesChannel(sdo.getSalesChannel());
        ofsActivityTermIndexLoadListVO.setSubItemId(sdo.getItemId());


       // QueryPageActivityTermIndexResponse queryPageActivityTermIndexResponse = ofsActivityTermIndexService.queryPageActivityTermIndex(ofsActivityTermIndexLoadListVO);


        //后续逻辑 todo...
        QueryPageActivityTermIndexResponse queryPageActivityTermIndexResponse = new QueryPageActivityTermIndexResponse();
        //如果输入了客户或者货品信息,先去查客户货品活动关系表,再根据结果决定查不查活动term ES表
        if (StringUtils.isNotBlank(ofsActivityTermIndexLoadListVO.getMainClientId()) || StringUtils.isNotBlank(
                ofsActivityTermIndexLoadListVO.getSubItemId())) {
            QueryActivityIdsByCustomerOrItemRequest customerOrItemRequest = new QueryActivityIdsByCustomerOrItemRequest();
            customerOrItemRequest.setCustomerId(ofsActivityTermIndexLoadListVO.getMainClientId());
            customerOrItemRequest.setItemId(ofsActivityTermIndexLoadListVO.getSubItemId());
            QueryActivityIdsByCustomerOrItemResponse customerOrItemResponse = ofsActivityTermRelationIndexService.
                    queryActivityIdsByCustomerOrItem(customerOrItemRequest);
            if (Objects.isNull(customerOrItemResponse) || CollectionUtils.isEmpty(customerOrItemResponse.getResult())) {
                queryPageActivityTermIndexResponse = ofsActivityTermIndexService.queryPageActivityTermIndex(ofsActivityTermIndexLoadListVO);
            }else{
                ofsActivityTermIndexLoadListVO.setRelationActivityList(customerOrItemResponse.getResult());
                if (StringUtils.isNotBlank(sdo.getActivityId())) {
                    ofsActivityTermIndexLoadListVO.setRelationActivityList(Lists.newArrayList(sdo.getActivityId()));
                }
                QueryPageActivityTermIndexReplenishResponse activityTermIndexReplenishResponse = ofsActivityTermIndexService.
                        queryPageActivityTermIndexReplenish(ofsActivityTermIndexLoadListVO);
                BeanUtils.copyProperties(activityTermIndexReplenishResponse,queryPageActivityTermIndexResponse);
            }

        } else {
            queryPageActivityTermIndexResponse = ofsActivityTermIndexService.queryPageActivityTermIndex(ofsActivityTermIndexLoadListVO);
        }

        return queryPageActivityTermIndexResponse;
    }

    @Override
    public QuerySalesOrganizationIdsResponse querySalesOrganizationIds(ActivityTermIndexSDO activityTermIndexSDO) {
        QuerySalesOrganizationIdsRequest querySalesOrganizationIdsRequest = new QuerySalesOrganizationIdsRequest();
        querySalesOrganizationIdsRequest.setStart(activityTermIndexSDO.getStart());
        querySalesOrganizationIdsRequest.setLimit(activityTermIndexSDO.getLimit());
        querySalesOrganizationIdsRequest.setCodeList(activityTermIndexSDO.getCodeList());
        QuerySalesOrganizationIdsResponse querySalesOrganizationIdsResponse = ofsActivityTermIndexService.querySalesOrganizationIds(querySalesOrganizationIdsRequest);
        return querySalesOrganizationIdsResponse;
    }

    @Override
    public QueryChannelIdsResponse queryChannelIds(ActivityTermIndexSDO activityTermIndexSDO) {
        QueryChannelIdsRequest queryChannelIdsRequest = new QueryChannelIdsRequest();
        queryChannelIdsRequest.setStart(activityTermIndexSDO.getStart());
        queryChannelIdsRequest.setLimit(activityTermIndexSDO.getLimit());
        QueryChannelIdsResponse queryChannelIdsResponse = ofsActivityTermIndexService.queryChannelIds(queryChannelIdsRequest);
        return queryChannelIdsResponse;
    }

    @Override
    public Result<List<ActivityTermViewResponse>> queryPromotionDetailPageList(ActivityTermSDO sdo) {
        return null;
    }

    @Override
    public LoadListResponse querySegmentDetailPageList(SegmentedActivityTermSDO sdo) {
        SegmentedActivityTermLoadListVO segmentedActivityTermLoadListVO = new SegmentedActivityTermLoadListVO();
        BeanUtils.copyProperties(sdo, segmentedActivityTermLoadListVO);
        LoadListResponse loadListResponse = segmentedActivityTermService.loadList(segmentedActivityTermLoadListVO);

        return loadListResponse;
    }

    @Override
    public QueryActivityUseRecordRelationResponse queryPromotionUsedDetailPageList(ActivityUseRecordRelationSDO activityUseRecordRelationSDO) {
        ActivityUseRecordRelationLoadListVO activityUseRecordRelationLoadListVO = new ActivityUseRecordRelationLoadListVO();
        BeanUtils.copyProperties(activityUseRecordRelationSDO, activityUseRecordRelationLoadListVO);
        activityUseRecordRelationLoadListVO.setStart(activityUseRecordRelationSDO.getStart());
        activityUseRecordRelationLoadListVO.setLimit(activityUseRecordRelationSDO.getLimit());
        QueryActivityUseRecordRelationResponse queryActivityUseRecordRelationResponse = activityUseRecordRelationService.queryActivityUseRecordRelation(activityUseRecordRelationLoadListVO);
        return queryActivityUseRecordRelationResponse;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public ActivityTitleSDO queryActivityTitleByActivityId(String activityId) {
        QueryActivityTitleByActivityIdRequest request = new QueryActivityTitleByActivityIdRequest();
        request.setActivityId(activityId);
        ActivityTitleLoadListVO activityTitleLoadListVO = activityTitleService.queryActivityTitleByActivityId(request);
        ActivityTitleSDO activityTitleSDO = new ActivityTitleSDO();
        BeanUtils.copyProperties(activityTitleLoadListVO, activityTitleSDO);
        return activityTitleSDO;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public ActivityTermSDO queryTermSDOListByTermId(String activityId, String termId) {
        QueryTermSDOListByTermIdRequest req = new QueryTermSDOListByTermIdRequest();
        req.setTermId(termId);
        req.setActivityId(activityId);
        ActivityTermLoadListVO termLoadListVO = activityTermService.queryTermSDOListByTermId(req);
        ActivityTermSDO termSDO = new ActivityTermSDO();
        BeanUtils.copyProperties(termLoadListVO, termSDO);
        return termSDO;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public List<SegmentedActivityTermSDO> querySegmentedActivityTermList(String activityId, String activityTermId) {
        QuerySegmentedActivityTermListRequest segmentedActivityTermListRequest = new QuerySegmentedActivityTermListRequest();
        segmentedActivityTermListRequest.setActivityTermId(activityTermId);
        segmentedActivityTermListRequest.setActivityId(activityId);
        QuerySegmentedActivityTermListResponse response = segmentedActivityTermService.querySegmentedActivityTermList(segmentedActivityTermListRequest);

        List<SegmentedActivityTermSDO> result = new ArrayList<>();
        if (Objects.nonNull(response) && CollectionUtils.isNotEmpty(response.getSegmentedActivityTermList())) {
            response.getSegmentedActivityTermList().forEach(e -> {
                SegmentedActivityTermSDO sdo = new SegmentedActivityTermSDO();
                BeanUtils.copyProperties(e, sdo);
                result.add(sdo);
            });
        }
        return result;
    }

    /**
     * 根据活动项目 Id 查询原品 id列表
     *
     * @param activityId     活动 ID
     * @param activityTermId 活动项目 ID
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public List<String> queryTermAndItemRelationByTermId(String activityId, String activityTermId) {
        QueryTermAndItemRelationByTermIdRequest request = new QueryTermAndItemRelationByTermIdRequest();
        request.setActivityId(activityId);
        request.setActivityTermId(activityTermId);
        QueryTermAndItemRelationByTermIdResponse termIdResponse = termItemRelationService.queryTermAndItemRelationByTermId(request);

        if (Objects.nonNull(termIdResponse) && CollectionUtils.isNotEmpty(termIdResponse.getItemList())) {
            return termIdResponse.getItemList();
        }
        List<String> result = new ArrayList<>();
        return result;
    }

    /**
     * 在活动 term 和赠品关系表中查询赠品列表
     *
     * @param activityId     活动 ID
     * @param activityTermId 活动项目 ID
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public List<String> queryTermAndGiftRelationList(String activityId, String activityTermId) {
        ActivityTermGiftRelationCreateRequest createRequest = new ActivityTermGiftRelationCreateRequest();
        createRequest.setActivityId(activityId);
        createRequest.setActivityTermId(activityTermId);
        QueryTermAndGiftRelationListResponse relationListResponse = termGiftRelationService.queryTermAndGiftRelationList(createRequest);
        List<String> result = new ArrayList<>();
        if (Objects.nonNull(relationListResponse) && CollectionUtils.isNotEmpty(relationListResponse.getItemIdList())) {
            relationListResponse.getItemIdList().forEach(e -> {
                result.add((String) e);
            });
        }
        return result;
    }

    @Override
    public QueryClientIdsResponse queryCustomerIds(ActivityTermIndexSDO activityTermIndexSDO) {
        QueryClientIdsRequest queryClientIdsRequest = new QueryClientIdsRequest();
        queryClientIdsRequest.setStart(activityTermIndexSDO.getStart());
        queryClientIdsRequest.setLimit(activityTermIndexSDO.getLimit());
        QueryClientIdsResponse queryClientIdsResponse = ofsActivityTermIndexService.queryClientIds(queryClientIdsRequest);
        return queryClientIdsResponse;

    }

    @Override
    public QueryItemIdsResponse queryItemIds(ActivityTermIndexSDO activityTermIndexSDO) {
        QueryItemIdsRequest queryItemIdsRequest = new QueryItemIdsRequest();
        queryItemIdsRequest.setStart(activityTermIndexSDO.getStart());
        queryItemIdsRequest.setLimit(activityTermIndexSDO.getLimit());
        QueryItemIdsResponse queryItemIdsResponse = ofsActivityTermIndexService.queryItemIds(queryItemIdsRequest);
        return queryItemIdsResponse;
    }


    @Override
    public Boolean checkUsedActivityRecord(CoverActivityInfoRequest activityInfo) {
        ActivityUseRecordRelationLoadListVO activityUseRecordRelationLoadListVO = new ActivityUseRecordRelationLoadListVO();
        BeanUtils.copyProperties(activityInfo, activityUseRecordRelationLoadListVO);
        QueryIsExistRecordResponse queryIsExistRecordResponse = activityUseRecordRelationService.queryIsExistRecord(activityUseRecordRelationLoadListVO);
        if (Objects.nonNull(queryIsExistRecordResponse) && CollectionUtils.isNotEmpty(queryIsExistRecordResponse.getResult())) {
            return true;
        }
        return false;
    }

    @Override
    public List<ActivityTermGiftRelation> queryActivityIdAndTermIdList(ActivityGiftItemRelationSDO activityGiftItemRelationSDO) {
        QueryActivityIdAndTermIdRequest queryActivityIdAndTermIdRequest = new QueryActivityIdAndTermIdRequest();
        queryActivityIdAndTermIdRequest.setGiftIds(activityGiftItemRelationSDO.getGiftIds());
        queryActivityIdAndTermIdRequest.setActivityTermId(activityGiftItemRelationSDO.getActivityTermId());
        QueryActivityIdAndTermIdResponse queryActivityIdAndTermIdResponse = termGiftRelationService.queryActivityIdAndTermId(queryActivityIdAndTermIdRequest);
        if (Objects.isNull(queryActivityIdAndTermIdResponse) || CollectionUtils.isEmpty(queryActivityIdAndTermIdResponse.getResult())) {
            return new ArrayList<>();
        }
        return queryActivityIdAndTermIdResponse.getResult();
    }

    @Override
    public ActivityUseRecordRelationSDO queryActivityUseRecord(ParticipateActivityInfoRequest request) {
        //查询该订单是否已经占用过该活动
        ActivityUseRecordRelationLoadListVO activityUseRecordRelationLoadListVO = new ActivityUseRecordRelationLoadListVO();
        activityUseRecordRelationLoadListVO.setActivityId(request.getActivityId());
        activityUseRecordRelationLoadListVO.setActivityTermId(request.getParticipateActivityTermRequest().getActivityTermId());
        activityUseRecordRelationLoadListVO.setMainOrderId(request.getMainOrderId());
        activityUseRecordRelationLoadListVO.setSubOrderId(request.getSubOrderId());
        activityUseRecordRelationLoadListVO.setItemId(request.getParticipateActivityTermRequest().getItem().getItemId());
        activityUseRecordRelationLoadListVO.setStatus(CommStatusEnum.ENABLE.getCode());
        activityUseRecordRelationLoadListVO.setSequenceNo(request.getSequenceNo());

        QueryIsExistRecordResponse queryIsExistRecordResponse = activityUseRecordRelationService.queryIsExistRecord(activityUseRecordRelationLoadListVO);

        if (Objects.nonNull(queryIsExistRecordResponse) && CollectionUtils.isNotEmpty(queryIsExistRecordResponse.getResult())) {
            log.info("当前订单存在活动占用记录 = {}", queryIsExistRecordResponse);
            ActivityUseRecordRelationSDO activityUseRecordRelationSDO = new ActivityUseRecordRelationSDO();
            BeanUtils.copyProperties(queryIsExistRecordResponse.getResult().get(0), activityUseRecordRelationSDO);
            return activityUseRecordRelationSDO;
        }
        return null;
    }

    /**
     * 查询坎级组合表（也叫坎级流水表，这个叫法是错误的，原因是最开始的时候，是这样叫的，现在已经改不了）
     *
     * @param activityId          活动 ID
     * @param activityTermId      活动项目 ID
     * @param segmentedActivityId 坎级活动 ID
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public List<SegmentedActivityCombinationSDO> querySegmentedActivityCombinationList(String activityId,
                                                                                       String activityTermId, String segmentedActivityId) {
        List<SegmentedActivityCombinationSDO> sdoList = new ArrayList<>();
        QuerySegmentedActivityCombinationReq req = new QuerySegmentedActivityCombinationReq();
        req.setActivityId(activityId);
        req.setActivityTermId(activityTermId);
        req.setSegmentedActivityId(segmentedActivityId);
        req.setLimit(10);
        req.setStart(0);
        Result<List<OperateJournalFlow>> listResult = operateJournalFlowService.querySegmentedActivityCombinationList(req);
        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            for (OperateJournalFlow flow : listResult.getResult()) {
                SegmentedActivityCombinationSDO sdo = new SegmentedActivityCombinationSDO();
                BeanUtils.copyProperties(flow, sdo);
                sdoList.add(sdo);
            }

            int total = listResult.getTotal();
            while (total > req.getStart() + req.getLimit()) {
                req.setStart(req.getStart() + req.getLimit());
                listResult = operateJournalFlowService.querySegmentedActivityCombinationList(req);
                if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
                    for (OperateJournalFlow flow : listResult.getResult()) {
                        SegmentedActivityCombinationSDO sdo = new SegmentedActivityCombinationSDO();
                        BeanUtils.copyProperties(flow, sdo);
                        sdoList.add(sdo);
                    }
                }
            }
        }
        return sdoList;
    }

    /**
     * 查询坎级组合表（也叫坎级流水表，这个叫法是错误的，原因是最开始的时候，是这样叫的，现在已经改不了）
     *
     * @param activityId     活动 ID
     * @param activityTermId 活动项目 ID
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public List<SegmentedActivityCombinationSDO> querySegmentedActivityCombinationList(String activityId, String activityTermId) {
        QuerySegmentedActivityCombinationListByItemIdReq idReq = new QuerySegmentedActivityCombinationListByItemIdReq();
        idReq.setActivityId(activityId);
        idReq.setActivityTermId(activityTermId);
//        idReq.setItemId(itemId);
        idReq.setStart(0);
        idReq.setLimit(10);
        List<SegmentedActivityCombinationSDO> sdoList = new ArrayList<>();
        Result<List<OperateJournalFlow>> listResult = operateJournalFlowService.querySegmentedActivityCombinationListByItemId(idReq);
        if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
            for (OperateJournalFlow flow : listResult.getResult()) {
                SegmentedActivityCombinationSDO sdo = new SegmentedActivityCombinationSDO();
                BeanUtils.copyProperties(flow, sdo);
                sdoList.add(sdo);
            }
            int total = listResult.getTotal();
            while (total > idReq.getStart() + idReq.getLimit()) {
                idReq.setStart(idReq.getStart() + idReq.getLimit());
                listResult = operateJournalFlowService.querySegmentedActivityCombinationListByItemId(idReq);
                if (Objects.nonNull(listResult) && CollectionUtils.isNotEmpty(listResult.getResult())) {
                    for (OperateJournalFlow flow : listResult.getResult()) {
                        SegmentedActivityCombinationSDO sdo = new SegmentedActivityCombinationSDO();
                        BeanUtils.copyProperties(flow, sdo);
                        sdoList.add(sdo);
                    }
                }
            }
        }
        return sdoList;
    }

    @Override
    public Result<List<String>> queryCustomerCodesByActivityId(ActivityTitleSDO activityTitleSDO) {
        QueryCustomerByActivityIdRequest activityTitleRequest = new QueryCustomerByActivityIdRequest();
        activityTitleRequest.setActivityId(activityTitleSDO.getActivityId());
        activityTitleRequest.setStart(activityTitleSDO.getStart());
        activityTitleRequest.setLimit(activityTitleSDO.getLimit());
        QueryCustomerByActivityIdResponse activityTitleResponse = activityTitleService.queryCustomerByActivityId(activityTitleRequest);

        com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryCustomerByActivityIdRequest clientRelationRequest = new com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryCustomerByActivityIdRequest();
        clientRelationRequest.setActivityId(activityTitleSDO.getActivityId());
        clientRelationRequest.setStart(activityTitleSDO.getStart());
        clientRelationRequest.setLimit(activityTitleSDO.getLimit());
        com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryCustomerByActivityIdResponse activityClientResponse = activityClientRelationService.queryCustomerByActivityId(clientRelationRequest);

        List<String> clientCodes = Lists.newArrayList();
        if (Objects.nonNull(activityTitleResponse) && CollectionUtils.isNotEmpty(activityTitleResponse.getResult())) {
            activityTitleResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) activityTitleResponse.getResult().stream().filter(item -> StringUtils.isNotBlank((String) item)).collect(Collectors.toSet())).stream().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                clientCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }

        if (Objects.nonNull(activityClientResponse) && CollectionUtils.isNotEmpty(activityClientResponse.getResult())) {
            activityClientResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) activityClientResponse.getResult().stream().filter(item -> StringUtils.isNotBlank((String) item)).collect(Collectors.toSet())).stream().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                clientCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }
        Result<List<String>> result = Result.success(clientCodes);
        result.setTotal(activityTitleResponse.getTotal() + activityClientResponse.getTotal());

        return result;
    }

    @Override
    public Result<List<String>> queryItemCodesByActivityId(ActivityTermSDO activityTermSDO) {
        QueryItemByActivityRequest queryItemByActivityRequest = new QueryItemByActivityRequest();
        BeanUtils.copyProperties(activityTermSDO, queryItemByActivityRequest);
        QueryItemByActivityResponse queryItemByActivityResponse = activityTermService.queryItemByActivity(queryItemByActivityRequest);

        QueryItemByActivityIdAndTermIdRequest queryItemByActivityIdAndTermIdRequest = new QueryItemByActivityIdAndTermIdRequest();
        BeanUtils.copyProperties(activityTermSDO, queryItemByActivityIdAndTermIdRequest);
        QueryItemByActivityIdAndTermIdResponse queryItemByActivityIdAndTermIdResponse = termItemRelationService.queryItemByActivityIdAndTermId(queryItemByActivityIdAndTermIdRequest);
        List<String> itemCodes = Lists.newArrayList();
        if (Objects.nonNull(queryItemByActivityResponse) && CollectionUtils.isNotEmpty(queryItemByActivityResponse.getResult())) {
            queryItemByActivityResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) queryItemByActivityResponse.getResult().stream().filter(item -> StringUtils.isNotBlank((String) item)).collect(Collectors.toSet())).stream().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                itemCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }

        if (Objects.nonNull(queryItemByActivityIdAndTermIdResponse) && CollectionUtils.isNotEmpty(queryItemByActivityIdAndTermIdResponse.getResult())) {
            queryItemByActivityIdAndTermIdResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) queryItemByActivityIdAndTermIdResponse.getResult().stream().filter(item -> StringUtils.isNotBlank((String) item)).collect(Collectors.toSet())).stream().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                itemCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }
        Result<List<String>> result = Result.success(itemCodes);
        result.setTotal(queryItemByActivityResponse.getTotal() + queryItemByActivityIdAndTermIdResponse.getTotal());

        return result;
    }

    @Override
    public Result<List<String>> queryGiftCodesByActivityId(ActivityTermSDO activityTermSDO) {
        QueryGiftByActivityRequest queryItemByActivityRequest = new QueryGiftByActivityRequest();
        BeanUtils.copyProperties(activityTermSDO, queryItemByActivityRequest);
        QueryGiftByActivityResponse queryGiftByActivityResponse = activityTermService.queryGiftByActivity(queryItemByActivityRequest);

        QueryGiftByActivityIdAndTermIdRequest queryGiftByActivityIdAndTermIdRequest = new QueryGiftByActivityIdAndTermIdRequest();
        BeanUtils.copyProperties(activityTermSDO, queryGiftByActivityIdAndTermIdRequest);
        QueryGiftByActivityIdAndTermIdResponse queryGiftByActivityIdAndTermIdResponse = termGiftRelationService.queryGiftByActivityIdAndTermId(queryGiftByActivityIdAndTermIdRequest);

        List<String> giftCodes = Lists.newArrayList();
        if (Objects.nonNull(queryGiftByActivityResponse) && CollectionUtils.isNotEmpty(queryGiftByActivityResponse.getResult())) {
            queryGiftByActivityResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) queryGiftByActivityResponse.getResult().stream().collect(Collectors.toSet())).stream().collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(collect)) {
                giftCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }

        if (Objects.nonNull(queryGiftByActivityIdAndTermIdResponse) && CollectionUtils.isNotEmpty(queryGiftByActivityIdAndTermIdResponse.getResult())) {
            queryGiftByActivityIdAndTermIdResponse.getResult().remove(null);
            List<String> collect = ((Set<String>) queryGiftByActivityIdAndTermIdResponse.getResult().stream().collect(Collectors.toSet())).stream().collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(collect)) {
                giftCodes.addAll(collect.stream().collect(Collectors.toList()));
            }
        }
        Result<List<String>> result = Result.success(giftCodes);
        result.setTotal(queryGiftByActivityResponse.getTotal() + queryGiftByActivityIdAndTermIdResponse.getTotal());

        return result;
    }

    @Override
    public List<ActivityTitle> queryActivityAndCustomerMap(List<String> activityIds) {
        List<ActivityTitle> list = Lists.newArrayList();
        //先查询活动抬头表
        QueryActivityAndCustomerMapRequest titleRequest = new QueryActivityAndCustomerMapRequest();
        titleRequest.setActivityIds(activityIds);
        QueryActivityAndCustomerMapResponse titleResponse = activityTitleService.queryActivityAndCustomerMap(titleRequest);
        if(Objects.nonNull(titleResponse) && CollectionUtils.isNotEmpty(titleResponse.getResult())){
            list.addAll(titleResponse.getResult());
        }

        //再查询活动客户关系表
        com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryActivityAndCustomerMapRequest relationRequest =
                new com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryActivityAndCustomerMapRequest();
        relationRequest.setActivityIds(activityIds);
        com.epoch.app.otsforecastsales.domian.activityclientrelation.dto.QueryActivityAndCustomerMapResponse relationResponse =
                activityClientRelationService.queryActivityAndCustomerMap(relationRequest);
        if(Objects.nonNull(relationResponse) && CollectionUtils.isNotEmpty(relationResponse.getResult())){
            list.addAll(relationResponse.getResult());
        }
        return list;
    }


    @Override
    public List<ActivityTitle> queryActivityAndCustomerMapForExport(List<String> activityIds) {
       return queryActivityAndCustomerMap(activityIds);
    }

    @Override
    public List<ActivityTerm> queryActivityAndItermMap(Map<String, String> activityMap) {
        List<ActivityTerm> list = Lists.newArrayList();

        //参数封装
        List<ActivityTerm> activityTerms = activityMap.entrySet().stream().map(e -> {
            ActivityTerm activityTerm = new ActivityTerm();
            activityTerm.setActivityTermId(e.getKey());
            activityTerm.setActivityId(e.getValue());
            return activityTerm;
        }).collect(Collectors.toList());

        //先查询活动项目表
        QueryActivityAndItemMapRequest termRequest = new QueryActivityAndItemMapRequest();
        termRequest.setActivitylist(activityTerms);
        QueryActivityAndItemMapResponse termResponse = activityTermService.queryActivityAndItemMap(termRequest);
        if(Objects.nonNull(termResponse) && CollectionUtils.isNotEmpty(termResponse.getResult())){
            list.addAll(termResponse.getResult());
        }

        //再查询活动原品关系表
        com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryActivityAndItemMapRequest itemRequest =
                new com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryActivityAndItemMapRequest();
        itemRequest.setActivitylist(activityTerms);
        com.epoch.app.otsforecastsales.domian.activitytermitemrelation.dto.QueryActivityAndItemMapResponse itemResponse =
                activityTermItemRelationService.queryActivityAndItemMap(itemRequest);
        if(Objects.nonNull(itemResponse) && CollectionUtils.isNotEmpty(itemResponse.getResult())){
            list.addAll(itemResponse.getResult());
        }
        return list;
    }


    @Override
    public List<ActivityTerm> queryActivityAndItermMapForExport(Map<String, String> activityMap) {
        List<ActivityTerm> list = Lists.newArrayList();

        //参数封装
        List<ActivityTerm> activityTerms = activityMap.entrySet().stream().map(e -> {
            ActivityTerm activityTerm = new ActivityTerm();
            activityTerm.setActivityTermId(e.getKey());
            activityTerm.setActivityId(e.getValue());
            return activityTerm;
        }).collect(Collectors.toList());

        //先查询活动项目表
        QueryActivityAndItemMapRequest termRequest = new QueryActivityAndItemMapRequest();
        termRequest.setActivitylist(activityTerms);
        QueryActivityAndItemMapResponse termResponse = activityTermService.queryActivityAndItemMap(termRequest);
        if (Objects.nonNull(termResponse) && CollectionUtils.isNotEmpty(termResponse.getResult())) {
            list.addAll(termResponse.getResult());
        }

        //再查询活动原品关系表
        QueryActivityAndItemMapForExportRequest itemRequest = new QueryActivityAndItemMapForExportRequest();
        itemRequest.setActivityTermIdList(activityMap.keySet().stream().collect(Collectors.toList()));
        itemRequest.setStart(0);
        itemRequest.setLimit(1);

        QueryActivityAndItemMapForExportResponse itemSubResponse = activityTermItemRelationService.queryActivityAndItemMapForExport(itemRequest);
        if (Objects.isNull(itemSubResponse) || CollectionUtils.isEmpty(itemSubResponse.getResult())) {
            return list;
        }

        itemRequest.setLimit(itemSubResponse.getTotal());
        QueryActivityAndItemMapForExportResponse itemResponse = activityTermItemRelationService.queryActivityAndItemMapForExport(itemRequest);
        if (Objects.isNull(itemResponse) || CollectionUtils.isEmpty(itemResponse.getResult())) {
            return list;
        }
        list.addAll(itemResponse.getResult());
        return list;
    }

    @Override
    public List<ActivityTerm> queryActivityAndGiftMap(Map<String, String> activityMap) {
        List<ActivityTerm> list = Lists.newArrayList();

        //参数封装
        List<ActivityTerm> activityTerms = activityMap.entrySet().stream().map(e -> {
            ActivityTerm activityTerm = new ActivityTerm();
            activityTerm.setActivityTermId(e.getKey());
            activityTerm.setActivityId(e.getValue());
            return activityTerm;
        }).collect(Collectors.toList());

        //先查询活动项目表
        QueryActivityAndItemMapRequest termRequest = new QueryActivityAndItemMapRequest();
        termRequest.setActivitylist(activityTerms);
        QueryActivityAndItemMapResponse termResponse = activityTermService.queryActivityAndItemMap(termRequest);
        if(Objects.nonNull(termResponse) && CollectionUtils.isNotEmpty(termResponse.getResult())){
            list.addAll(termResponse.getResult());
        }

        //再查询活动赠品关系表
        QueryActivityAndGiftMapRequest giftRequest =new QueryActivityAndGiftMapRequest();
        giftRequest.setActivitylist(activityTerms);
        QueryActivityAndGiftMapResponse gifResponse = activityTermGiftRelationService.queryActivityAndGiftMap(giftRequest);
        if(Objects.nonNull(gifResponse) && CollectionUtils.isNotEmpty(gifResponse.getResult())){
            list.addAll(gifResponse.getResult());
        }
        return list;
    }

    @Override
    public List<ActivityTerm> queryActivityAndGiftMapForExport(Map<String, String> activityMap) {
        List<ActivityTerm> list = Lists.newArrayList();

        //参数封装
        List<ActivityTerm> activityTerms = activityMap.entrySet().stream().map(e -> {
            ActivityTerm activityTerm = new ActivityTerm();
            activityTerm.setActivityTermId(e.getKey());
            activityTerm.setActivityId(e.getValue());
            return activityTerm;
        }).collect(Collectors.toList());

        //先查询活动项目表
        QueryActivityAndItemMapRequest termRequest = new QueryActivityAndItemMapRequest();
        termRequest.setActivitylist(activityTerms);
        QueryActivityAndItemMapResponse termResponse = activityTermService.queryActivityAndItemMap(termRequest);
        if (Objects.isNull(termResponse) || CollectionUtils.isEmpty(termResponse.getResult())) {
            return list;
        }
        list.addAll(termResponse.getResult());

        //再查询活动赠品关系表
        QueryActivityAndGiftMapForExportRequest giftRequest = new QueryActivityAndGiftMapForExportRequest();
        giftRequest.setActivityTermIdList(activityMap.keySet().stream().collect(Collectors.toList()));
        giftRequest.setStart(0);
        giftRequest.setLimit(1);
        QueryActivityAndGiftMapForExportResponse giftSubResponse = activityTermGiftRelationService.queryActivityAndGiftMapForExport(giftRequest);
        if (Objects.isNull(giftSubResponse) || CollectionUtils.isEmpty(giftSubResponse.getResult())) {
            return list;
        }
        giftRequest.setLimit(giftSubResponse.getTotal());
        QueryActivityAndGiftMapForExportResponse giftResponse = activityTermGiftRelationService.queryActivityAndGiftMapForExport(giftRequest);
        if (Objects.isNull(giftResponse) || CollectionUtils.isEmpty(giftResponse.getResult())) {
            return list;
        }
        list.addAll(giftResponse.getResult());
        return list;
    }

    @Override
    public List<String> queryGroupControlActivityIds(List<String> activityIds) {
        QueryGroupControlActivityIdsRequest request = new QueryGroupControlActivityIdsRequest();
        request.setActivityIds(activityIds);
        request.setStart(0);
        request.setLimit(activityIds.size());
        QueryGroupControlActivityIdsResponse response = activityTitleService.queryGroupControlActivityIds(request);
        if (Objects.isNull(request) || CollectionUtils.isEmpty(response.getActivityIds())) {
            return new ArrayList<>();
        }
        return response.getActivityIds();
    }

    @Override
    public List<ActivityTitle> queryGroupControlActivityIdAndNoteId(Set<String> activityIds) {

        QueryGroupControlActivityIdAndNoteIdRequest request = new QueryGroupControlActivityIdAndNoteIdRequest();
        request.setActivityIds(Lists.newArrayList(activityIds));
        request.setStart(0);
        request.setLimit(activityIds.size());
        Result<List<ActivityTitle>> result = activityTitleService.queryGroupControlActivityIdAndNoteId(request);
        if(Objects.isNull(result) || CollectionUtils.isEmpty(result.getResult())){
            return new ArrayList<>();
        }
        return result.getResult();
    }

    @Override
    public List<ActivityTerm> queryActivityIdsByActivityTermIds(List<String> activityTermIds) {
        QueryActivityIdsByActivityTermIdsRequest request = new QueryActivityIdsByActivityTermIdsRequest();
        request.setActivityTermIds(activityTermIds);
        Result<List<ActivityTerm>> listResult = activityTermService.queryActivityIdsByActivityTermIds(request);
        if(Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())){
            return null;
        }
        return listResult.getResult();
    }
}
